<template>
  <div>
    <el-col :offset="9" :span="8">
      <el-input v-model="addStudentInfo.name" placeholder="学生姓名"></el-input>
      <el-input v-model="addStudentInfo.gender" placeholder="性别(男填1，女填2)" type="number"></el-input>
      <el-input v-model="addStudentInfo.age" placeholder="学生年龄" type="number"></el-input>
      <el-input v-model="addStudentInfo.studentId" placeholder="学生学号" type="number"></el-input>
      <el-button type="primary" @click="addStudent">添加学生</el-button>
    </el-col>
    <el-button type="danger" @click="deleteSelectedStudents" class="select-button">批量删除</el-button>
    <el-table :data="students" style="width: 100%" :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }" @selection-change="handleSelectionChange">
      <el-table-column prop="id" label="学生ID"></el-table-column>
      <el-table-column prop="name" label="学生姓名"></el-table-column>
      <el-table-column prop="gender" label="性别(1男，2女)"></el-table-column>
      <el-table-column prop="age" label="学生年龄"></el-table-column>
      <el-table-column prop="studentId" label="学生学号"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button type="primary" @click="editStudent(scope.row)">修改</el-button>
          <el-button type="danger" @click="deleteStudent(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="115"></el-table-column>
    </el-table>

    <el-dialog v-model="dialogStudentInfoVisible" title="修改学生信息" width="40%" top="20vh" center>
      <el-form :model="editStudentInfo" label-width="100px" ref="form">
        <el-form-item label="学生ID" prop="id">
          <el-input v-model="editStudentInfo.id" placeholder="请输入学生ID" type="number"></el-input>
        </el-form-item>
        <el-form-item label="学生姓名" prop="name">
          <el-input v-model="editStudentInfo.name" placeholder="请输入学生姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别(男1,女2)" prop="gender">
          <el-input v-model="editStudentInfo.gender" placeholder="请输入学生性别(男填1，女填2)" type="number"></el-input>
        </el-form-item>
        <el-form-item label="学生年龄" prop="age">
          <el-input v-model="editStudentInfo.age" placeholder="请输入学生年龄" type="number"></el-input>
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input v-model="editStudentInfo.password" placeholder="请输入学生密码" show-password></el-input>
        </el-form-item>
        <el-form-item label="学生学号" prop="studentId">
          <el-input v-model="editStudentInfo.studentId" placeholder="请输入学生学号" type="number"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogStudentInfoVisible = false">取消修改</el-button>
          <el-button type="primary" @click="handleEdit()">
            确认修改
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { getAllStudentInfoService, addStudentInfoService, deleteStudentInfoService, updateStudentInfoService } from '@/api/manager.js'
import { ElMessage, ElMessageBox } from 'element-plus'
const students = ref([]);
const dialogStudentInfoVisible = ref(false);
const delarr = ref([]);
const handleSelectionChange = (val) => {
  delarr.value = val
}
const addStudentInfo = reactive({
  name: '',
  gender: '',
  age: '',
  studentId: ''
});
//展示所有课程信息
const getStudentInfo = async () => {
  const res = await getAllStudentInfoService()
  students.value = res.data
}
getStudentInfo()
const addStudent = async () => {
  if (addStudentInfo.name && addStudentInfo.gender && addStudentInfo.age && addStudentInfo.studentId) {
    await addStudentInfoService(addStudentInfo)
    getStudentInfo()
    ElMessage.success('添加成功!')
    addStudentInfo.name = ''
    addStudentInfo.gender = ''
    addStudentInfo.age = ''
    addStudentInfo.studentId = ''

  } else {
    ElMessage.error('请填写完整信息!')
  }
};

const editStudentInfo = reactive({
  name: '',
  gender: '',
  age: '',
  studentId: '',
  password: '',
  id: ''
});
const editStudent = async (course) => {
  dialogStudentInfoVisible.value = true
  editStudentInfo.name = course.name
  editStudentInfo.gender = course.gender
  editStudentInfo.age = course.age
  editStudentInfo.studentId = course.studentId
  editStudentInfo.password = course.password
  editStudentInfo.id = course.id
};
const handleEdit = async () => {
  if (editStudentInfo.name && editStudentInfo.gender && editStudentInfo.age && editStudentInfo.studentId && editStudentInfo.password && editStudentInfo.id) {
    const res = await updateStudentInfoService(editStudentInfo)
    getStudentInfo()
    ElMessage.success('修改成功!')
    dialogStudentInfoVisible.value = false
    editStudentInfo.name = ''
    editStudentInfo.gender = ''
    editStudentInfo.age = ''
    editStudentInfo.studentId = ''
    editStudentInfo.password = ''
    editStudentInfo.id = ''
  } else {
    ElMessage.error('请填写完整信息!')
  }

}
const deleteStudent = async (student) => {
  console.log(student)
  await ElMessageBox.confirm('你确认要删除该学生么', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await deleteStudentInfoService([student.id])
  ElMessage.success('删除成功!')
  getStudentInfo()

};
const deleteSelectedStudents = async () => {
  await ElMessageBox.confirm('你确认要删除这些学生么', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  const res = await deleteStudentInfoService(delarr.value.map(item => item.id))
  ElMessage.success('删除成功!')
  getStudentInfo()
}
</script>
<style scoped>
.el-input {
  margin: 10px 0;
}

.select-button {
  margin-left: 1750px;
}

:deep(.el-form-item__label) {
  padding: 9px 12px 0 0
}
</style>